﻿package hapi.web;

class RouteManager<T> implements RouteResolver<T> {
	
	var mappings:Array<{matcher:PathMatcher, value:T}>;
	var matcherFactory:PathMatcherFactory;
	
	public function new() {
		mappings = new Array();
		matcherFactory = new PrettyPathMatcherFactory();
	}
	
	public function map(path, target:T) {
		mappings.push( { matcher:this.matcherFactory.create(path), value:target });
	}
	
	public function to(value:T, ?params:{} ):String {
		for ( e in mappings) {
			if (value == e.value && e.matcher.paramsMatch(params)) {
				return e.matcher.resolve(params);
			}
		}
		return "";
	}
	
	public function from(uri:String):{value:T, params:Dynamic} {
		var params:Dynamic;
		for (e in mappings) {
			params = {};
			if (e.matcher.match(uri, params)) {
				return {
					value: e.value,
					params: params
				};
			}
		}
		return {value:null, params:null};
	}
}